package com.zxy.furns.web;/**
 * ClassName: CartServlet
 * Package: com.zxy.furns.web
 *
 * @Author zxy
 * @Create 2025/3/24 22:53
 * @Version 1.0
 * title:
 * Description:
 */

import com.google.gson.Gson;
import com.zxy.furns.entity.Cart;
import com.zxy.furns.entity.CartItem;
import com.zxy.furns.entity.Furn;
import com.zxy.furns.service.FurnService;
import com.zxy.furns.service.impl.FurnServiceImpl;
import com.zxy.furns.utils.DataUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/cartServlet")
public class CartServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();

    //    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int id = DataUtils.parseInt(req.getParameter("id"), 0);
//        Furn furn = furnService.queryFurnById(id);
//        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());
//        Cart cart = (Cart) req.getSession().getAttribute("cart");
//        if (cart == null) {
//            cart = new Cart();
//            req.getSession().setAttribute("cart", cart);
//        }
//        if (furn.getStock() > 0) {
//            cart.addItem(cartItem);
//        }
//
//        System.out.println("cart = " + cart);
//        String referer = req.getHeader("Referer");
//        resp.sendRedirect(referer);
//
//    }
    protected void addItemByAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        Furn furn = furnService.queryFurnById(id);
        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        if (furn.getStock() > 0) {
            cart.addItem(cartItem);
        }

        //添加完毕后，返回json数据
        //规定格式{"cartTotalCount",3}
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("cartTotalCount", cart.getTotalCount());
        Gson gson = new Gson();
        String resultJson = gson.toJson(resultMap);
        resp.getWriter().write(resultJson);
        System.out.println("cart = " + cart);


    }


    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        int count = DataUtils.parseInt(req.getParameter("count"), 1);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.updateCount(id, count);
        }
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    protected void delItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.delItem(id);
        }
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    protected void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clearItem();
        }
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }
}